<?php

/**
 * usuario actions.
 *
 * @package    inmobiliaria
 * @subpackage usuario
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class usuarioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->redirect('usuario/show');
  }

  public function executeTerms(sfWebRequest $request)
  {
   
  }

  public function executeShow(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      $this->perfil = Doctrine_Core::getTable('Perfil')->find(array($this->getUser()->getId()));
    }
    $this->forward404Unless($this->perfil);
  }

  public function executeNew(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      $this->redirect('usuario/show');
    }else{
      $this->form = new PerfilForm();
    }
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new PerfilForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      $this->perfil = Doctrine_Core::getTable('Perfil')->find(array($this->getUser()->getId()));
    }
    $this->forward404Unless($this->perfil);
    $this->form = new PerfilForm($this->perfil);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    if ($this->getUser()->isAuthenticated())
    {
      $this->perfil = Doctrine_Core::getTable('Perfil')->find(array($this->getUser()->getId()));
    }
    $this->forward404Unless($this->perfil);
    $this->form = new PerfilForm($this->perfil);
    $this->setTemplate('edit');
  }

  /*public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sf_guard_user = Doctrine_Core::getTable('sfGuardUser')->find(array($request->getParameter('id'))), sprintf('Object sf_guard_user does not exist (%s).', $request->getParameter('id')));
    $sf_guard_user->delete();

    $this->redirect('usuario/index');
  }*/

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind(
      $request->getParameter($form->getName()), 
      $request->getFiles($form->getName())
    );
    if ($form->isValid())
    {
      $sf_guard_user = $form->save();
      $this->redirect('usuario/edit');
    }
  }
}
